package com.lxs.mall.controller;

import com.github.pagehelper.PageInfo;
import com.lxs.mall.common.ApiRestResponse;
import com.lxs.mall.common.Constant;
import com.lxs.mall.model.pojo.Product;
import com.lxs.mall.model.request.AddProductReq;
import com.lxs.mall.model.request.UpdateProductReq;
import com.lxs.mall.service.ProductService;
import com.lxs.mall.service.UploadService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 后台商品管理
 *
 * @author 小松
 */
@Controller
@Validated
public class ProductAdminController {
    @Resource
    ProductService productService;
    @Resource
    UploadService uploadService;

    @ApiOperation("后台添加商品")
    @PostMapping("/admin/product/add")
    @ResponseBody
    public ApiRestResponse addProduct(@Valid @RequestBody AddProductReq addProductReq) {
        productService.add(addProductReq);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台上传商品图片")
    @PostMapping("/admin/upload/file")
    @ResponseBody
    public ApiRestResponse upload(HttpServletRequest httpServletRequest,
                                  @RequestParam("file") MultipartFile file) {
        String result = uploadService.uploadFile(file);
        return ApiRestResponse.success(result);
    }

    @ApiOperation("后台更新商品")
    @PostMapping("/admin/product/update")
    @ResponseBody
    public ApiRestResponse updateProduct(@Valid @RequestBody UpdateProductReq updateProductReq) {
        productService.update(updateProductReq);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台删除商品")
    @PostMapping("/admin/product/delete")
    @ResponseBody
    public ApiRestResponse deleteProduct(@RequestParam Integer id) {
        productService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台批量上下架")
    @PostMapping("/admin/product/batchUpdateSellStatus")
    @ResponseBody
    public ApiRestResponse batchUpdateSellStatus(
            @RequestParam Integer[] ids, @RequestParam Integer sellStatus) {
        productService.batchUpdateSellStatus(ids, sellStatus);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台商品列表")
    @GetMapping("/admin/product/list")
    @ResponseBody
    public ApiRestResponse list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo<Product> pageInfo = productService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("excel后台批量上传商品")
    @PostMapping("/admin/upload/product")
    @ResponseBody
    public ApiRestResponse uploadBatch(@RequestParam("file") MultipartFile file) throws IOException {
        String newFileName = uploadService.getNewFileName(file);
        File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
        uploadService.createImage(fileDirectory, file, destFile);
        productService.addProductByExcel(destFile);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台上传商品图片")
    @PostMapping("/admin/upload/image")
    @ResponseBody
    public ApiRestResponse uploadImage(HttpServletRequest httpServletRequest,
                                       @RequestParam("file") MultipartFile file) throws IOException {
        String result = uploadService.uploadImage(file);
        return ApiRestResponse.success(result);
    }

    @ApiOperation("后台批量上传商品")
    @PostMapping("/admin/product/batchList")
    @ResponseBody
    public ApiRestResponse batchList(@Valid @RequestBody List<AddProductReq> addProductReqList) {
        for (AddProductReq addProductReq : addProductReqList) {
            productService.add(addProductReq);
        }
        return ApiRestResponse.success();
    }
}
